Ext.ns("App");
var PortalItem = function(a, b, c) {
	this.panelid = a;
	this.colno = b;
	this.rowno = c;
};
var UserInfo = function(d, a, c, f, e, b) {
	this.userId = d;
	this.fullname = a;
	this.depId = c;
	this.depName = f;
	this.rights = e;
	this.portalConfig = b;
};
/**
 * 将布局信息存储于cookies中，使得保存布局后不需要重新登陆即可保持布局，
 * 
 * 如果cookies为空，则从用户sesssion中取的布局信息
 */
if (getCookie("portals")) {
	var curUserInfo = {};
	curUserInfo.portalConfig = getCookie("portals");
} else {
	var curUserInfo = _curUser;
}
function isGranted(a) {
	if (curUserInfo.rights.indexOf("__ALL") != -1) {
		return true;
	}
	if (curUserInfo.rights.indexOf(a) != -1) {
		return true;
	}
	return false;
}
// portal 1
Portal1View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal1View.superclass.constructor.call(this, {
							id : "Portal1View",
							title : "面板1",
							iconCls : "menu-diary",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath + "/myInfo.do?action=before"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext.getCmp("Portal1View").getUpdater()
								.update(_ctxPath + "/myInfo.do?action=before");
					}
				}, {
					id : "close",
					handler : function(c, b, a) {
						Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
									if (d == "yes") {
										a.ownerCt.remove(a, true);
									}
								});
					}
				}];
			}
		});
// portal 2
Portal2View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal2View.superclass.constructor.call(this, {
							id : "Portal2View",
							title : "面板2",
							iconCls : "menu-news",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath + "/zhltx.do"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext.getCmp("Portal2View").getUpdater()
								.update(_ctxPath + "/zhltx.do");
					}
				}];
			}
		});
// portal3
Portal3View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal3View.superclass.constructor.call(this, {
							id : "Portal3View",
							title : "面板3",
							iconCls : "menu-message",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath + "/zhlxslist.do"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext.getCmp("Portal3View").getUpdater()
								.update(_ctxPath + "/zhlxslist.do");
					}
				}];
			}
		});
Portal4View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal4View.superclass.constructor.call(this, {
							id : "Portal4View",
							title : "面板4",
							iconCls : "menu-notice",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath + "/moduleAdmin.do"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext.getCmp("Portal4View").getUpdater()
								.update(_ctxPath + "/moduleAdmin.do");
					}
				}, {
					id : "close",
					handler : function(c, b, a) {
						Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
									if (d == "yes") {
										a.ownerCt.remove(a, true);
									}
								});
					}
				}];
			}
		});
Portal5View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal5View.superclass.constructor.call(this, {
							id : "Portal5View",
							title : "面板5",
							iconCls : "menu-flowWait",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath + "/Admin/proxool/"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext.getCmp("Portal5View").getUpdater()
								.update(_ctxPath + "/Admin/proxool/?alias=MyPool&tab=snapshot&detail=more");
					}
				}, {
					id : "close",
					handler : function(c, b, a) {
						Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
									if (d == "yes") {
										a.ownerCt.remove(a, true);
									}
								});
					}
				}];
			}
		});
Portal6View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal6View.superclass.constructor.call(this, {
							id : "Portal6View",
							title : "面板6",
							iconCls : "menu-appointment",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath
										+ "/task/displayAppointment.do?start=0&limit=8"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext
								.getCmp("Portal6View")
								.getUpdater()
								.update(_ctxPath
										+ "/task/displayAppointment.do?start=0&limit=8");
					}
				}, {
					id : "close",
					handler : function(c, b, a) {
						Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
									if (d == "yes") {
										a.ownerCt.remove(a, true);
									}
								});
					}
				}];
			}
		});
Portal7View = Ext.extend(Ext.ux.Portlet, {
	tools : null,
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initTool();
		Portal7View.superclass.constructor.call(this, {
					id : "Portal7View",
					title : "面板7",
					iconCls : "menu-cal-plan-view",
					tools : this.tools,
					autoLoad : {
						url : _ctxPath
								+ "/task/displayCalendarPlan.do?start=0&limit=8"
					}
				});
	},
	initTool : function() {
		this.tools = [{
			id : "refresh",
			handler : function() {
				Ext
						.getCmp("Portal7View")
						.getUpdater()
						.update(_ctxPath
								+ "/task/displayCalendarPlan.do?start=0&limit=8");
			}
		}, {
			id : "close",
			handler : function(c, b, a) {
				Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
							if (d == "yes") {
								a.ownerCt.remove(a, true);
							}
						});
			}
		}];
	}
});
Portal9View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal9View.superclass.constructor.call(this, {
							id : "Portal9View",
							title : "面板9",
							iconCls : "menu-myplan",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath
										+ "/task/displayWorkPlan.do?start=0&limit=8"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext
								.getCmp("Portal9View")
								.getUpdater()
								.update(_ctxPath
										+ "/task/displayWorkPlan.do?start=0&limit=8");
					}
				}, {
					id : "close",
					handler : function(c, b, a) {
						Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
									if (d == "yes") {
										a.ownerCt.remove(a, true);
									}
								});
					}
				}];
			}
		});
Portal10View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal10View.superclass.constructor.call(this, {
							id : "Portal10View",
							title : "面板10",
							iconCls : "menu-news",
							tools : this.tools,
							border : false,
							height : 330,
							autoLoad : {
								url : _ctxPath + "/info/imageNews.do",
								scripts : true
							}
						});
			},
			initTool : function() {
				this.tools = [{
							id : "refresh",
							handler : function() {
								Ext.getCmp("Portal10View").getUpdater()
										.update({
											url : _ctxPath
													+ "/info/imageNews.do",
											scripts : true
										});
							}
						}, {
							id : "close",
							handler : function(c, b, a) {
								Ext.Msg.confirm("提示信息", "确认删除此模块吧？",
										function(d) {
											if (d == "yes") {
												a.ownerCt.remove(a, true);
											}
										});
							}
						}];
			}
		});
Portal11View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal11View.superclass.constructor.call(this, {
							id : "Portal11View",
							title : "滚动面板11",
							iconCls : "menu-notice",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath + "/pages/indexpages/noticeScrollPage.jsp",
								scripts : true
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext.getCmp("Portal11View").getUpdater().update(
								{
									url : _ctxPath + "/pages/indexpages/noticeScrollPage.jsp",
									scripts : true
								});
					}
				}, {
					id : "close",
					handler : function(c, b, a) {
						Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
									if (d == "yes") {
										a.ownerCt.remove(a, true);
									}
								});
					}
				}];
			}
		});
Portal12View = Ext.extend(Ext.ux.Portlet, {
	tools : null,
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initTool();
		Portal12View.superclass.constructor.call(this, {
					id : "Portal12View",
					title : "面板12",
					iconCls : "menu-document",
					tools : this.tools,
					autoLoad : {
						url : _ctxPath
								+ "/document/displayDocument.do?start=0&limit=8"
					}
				});
	},
	initTool : function() {
		this.tools = [{
			id : "refresh",
			handler : function() {
				Ext.getCmp("Portal12View").getUpdater().update(_ctxPath
						+ "/document/displayDocument.do?start=0&limit=8");
			}
		}, {
			id : "close",
			handler : function(c, b, a) {
				Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
							if (d == "yes") {
								a.ownerCt.remove(a, true);
							}
						});
			}
		}];
	}
});
Portal13View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal13View.superclass.constructor.call(this, {
							id : "Portal13View",
							title : "面板13",
							iconCls : "menu-mail",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath
										+ "/communicate/displayMail.do?start=0&limit=8"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext
								.getCmp("Portal13View")
								.getUpdater()
								.update(_ctxPath
										+ "/communicate/displayMail.do?start=0&limit=8");
					}
				}, {
					id : "close",
					handler : function(c, b, a) {
						Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
									if (d == "yes") {
										a.ownerCt.remove(a, true);
									}
								});
					}
				}];
			}
		});
Portal8View = Ext.extend(Ext.ux.Portlet, {
			tools : null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initTool();
				Portal8View.superclass.constructor.call(this, {
							id : "Portal8View",
							title : "面板8",
							iconCls : "menu-depplan",
							tools : this.tools,
							autoLoad : {
								url : _ctxPath + "/task/displayDepWorkPlan.do"
							}
						});
			},
			initTool : function() {
				this.tools = [{
					id : "refresh",
					handler : function() {
						Ext.getCmp("Portal8View").getUpdater()
								.update(_ctxPath
										+ "/task/displayDepWorkPlan.do");
					}
				}, {
					id : "close",
					handler : function(c, b, a) {
						Ext.Msg.confirm("提示信息", "确认删除此模块吧？", function(d) {
									if (d == "yes") {
										a.ownerCt.remove(a, true);
									}
								});
					}
				}];
			}
		});
PanelSelectorWin = Ext.extend(Ext.Window, {
	formPanel : null,
	buttons : null,
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUI();
		PanelSelectorWin.superclass.constructor.call(this, {
					id : "PanelSelectorWin",
					title : "选择显示模块",
					layout : "fit",
					height : 220,
					width : 300,
					modal : true,
					defaults : {
						padding : "5"
					},
					buttons : this.buttons,
					buttonAlign : "center",
					items : this.formPanel
				});
	},
	initUI : function() {
		this.formPanel = new Ext.FormPanel({
					id : "PanelSelectorForm",
					layout : "column",
					items : [{
								layout : "form",
								columnWidth : 0.5,
								border : false,
								items : [{
											xtype : "checkbox",
											boxLabel : "面板5",
											hideLabel : true,
											id : "Portal5ViewCheckBox",
											name : "Portal5View"
										}, {
											xtype : "checkbox",
											boxLabel : "面板1",
											hideLabel : true,
											id : "Portal1ViewCheckBox",
											name : "Portal1View"
										}, {
											xtype : "checkbox",
											hideLabel : true,
											boxLabel : "面板6",
											id : "Portal6ViewCheckBox",
											name : "Portal6View"
										}, {
											xtype : "checkbox",
											boxLabel : "面板7",
											hideLabel : true,
											id : "Portal7ViewCheckBox",
											name : "Portal7View"
										}, {
											xtype : "checkbox",
											boxLabel : "面板8",
											hideLabel : true,
											id : "Portal8ViewCheckBox",
											name : "Portal8View"
										}, {
											xtype : "checkbox",
											boxLabel : "面板4（列表显示）",
											hideLabel : true,
											id : "Portal4ViewCheckBox",
											name : "Portal4View"
										}]
							}, {
								layout : "form",
								columnWidth : 0.5,
								border : false,
								items : [{
											xtype : "checkbox",
											hideLabel : true,
											boxLabel : "面板9",
											id : "Portal9ViewCheckBox",
											name : "Portal9View"
										}, {
											xtype : "checkbox",
											boxLabel : "面板10",
											hideLabel : true,
											id : "Portal10ViewCheckBox",
											name : "Portal10View"
										}, {
											xtype : "checkbox",
											boxLabel : "滚动面板11",
											hideLabel : true,
											id : "Portal11ViewCheckBox",
											name : "Portal11View"
										}, {
											xtype : "checkbox",
											boxLabel : "面板12",
											hideLabel : true,
											id : "Portal12ViewCheckBox",
											name : "Portal12View"
										}, {
											xtype : "checkbox",
											boxLabel : "面板13",
											hideLabel : true,
											id : "Portal13ViewCheckBox",
											name : "Portal13View"
										}]
							}]
				});
		var portal = Ext.getCmp("Portal");
		curUserInfo.portalConfig = [];
		var items = portal.items;
		for (var i = 0; i < items.length; i++) {
			var v = items.itemAt(i);
			for (var j = 0; j < v.items.getCount(); j++) {
				var m = v.items.itemAt(j);
				var portalItem = new PortalItem(m.id, i, j);
				curUserInfo.portalConfig.push(portalItem);
			}
		}
		var confs = curUserInfo.portalConfig;
		for (var i = 0; i < confs.length; i++) {
			var panelView = confs[i].panelid;
			var panelCheck = Ext.getCmp(panelView + "CheckBox");
			if (panelCheck != null) {
				panelCheck.setValue(true);
				panelCheck.disable();
			}
		}
		this.buttons = [{
			xtype : "button",
			text : "确定",
			iconCls : "btn-save",
			handler : function() {
				var fd = Ext.getCmp("PortalItem");
				var portal = Ext.getCmp("Portal");
				var array = ["Portal1View", "Portal5View",
						"Portal4View", "Portal6View",
						"Portal7View", "Portal8View",
						"Portal9View", "Portal10View",
						"Portal11View", "Portal12View",
						"Portal13View"];
				for (var v = 0; v < array.length; v++) {
					var check = Ext.getCmp(array[v] + "CheckBox");
					if (check != null) {
						if (check.getValue() && Ext.getCmp(array[v]) == null) {
							var panel = eval("new " + array[v] + "()");
							fd.add(panel);
						}
					}
				}
				fd.doLayout();
				portal.doLayout();
				Ext.getCmp("PanelSelectorWin").close();
			}
		}, {
			xtype : "button",
			text : "取消",
			iconCls : "btn-cancel",
			handler : function() {
				Ext.getCmp("PanelSelectorWin").close();
			}
		}];
	}
});
AppHome = Ext.extend(Ext.Panel, {
	portalPanel : null,
	toolbar : null,
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUIComponents();
		AppHome.superclass.constructor.call(this, {
					// title : "首 页",
					closable : false,
					id : "AppHome",
					iconCls : "menu-company",
					layout : "fit",
					defaults : {
						padding : "0 5 0 0"
					},
					tbar : this.toolbar,
					items : this.portalPanel
				});
	},
	initUIComponents : function() {
		this.toolbar = new Ext.Toolbar({
			height : 30,
			items : ["->", {
						xtype : "button",
						text : "添加模块",
						iconCls : "btn-add",
						handler : function() {
							new PanelSelectorWin().show();
						}
					}, {
						xtype : "button",
						text : "保存布局",
						iconCls : "btn-save",
						handler : function() {
							var portal = Ext.getCmp("Portal");
							curUserInfo.portalConfig = [];
							var items = portal.items;
							for (var i = 0; i < items.length; i++) {
								var v = items.itemAt(i);
								for (var j = 0; j < v.items.getCount(); j++) {
									var m = v.items.itemAt(j);
									var portalItem = new PortalItem(m.id, i, j);
									curUserInfo.portalConfig.push(portalItem);
								}
							}
							Ext.Ajax.request({
								method : "post",
								url : _ctxPath + "/savePortal.do?op=ajax",
								success : function(request) {
									setCookie(
											"portals",
											Ext
													.encode(curUserInfo.portalConfig));
									Ext.ux.Toast.msg("操作信息", "保存成功");
								},
								failure : function(request) {
									Ext.MessageBox.show({
												title : "操作信息",
												msg : "信息保存出错，请联系管理员！",
												buttons : Ext.MessageBox.OK,
												icon : "ext-mb-error"
											});
								},
								params : {
									items : Ext
											.encode(curUserInfo.portalConfig)
								}
							});
						}
					}]
		});
		var tools = [{
					id : "gear",
					handler : function() {
						Ext.Msg.alert("Message",
								"The Settings tool was clicked.");
					}
				}, {
					id : "close",
					handler : function(e, target, panel) {
						panel.ownerCt.remove(panel, true);
					}
				}];
		var confs = Ext.decode(curUserInfo.portalConfig);
		if (confs == null || confs == undefined || confs.length < 1) {
			confs = new Array();
			var p1 = {
				panelid : "Portal3View",
				colno : 1,
				rowno : 0
			};
			var p2 = {
				panelid : "Portal4View",
				colno : 0,
				rowno : 1
			};
			var p3 = {
				panelid : "Portal2View",
				colno : 0,
				rowno : 0
			};
			confs.push(p3);
			confs.push(p2);
			confs.push(p1);
		}
		var column0 = [];
		var column1 = [];
		for (var v = 0; v < confs.length; v++) {
			var o = confs[v];
			// alert("new " + confs[v].panelid + "()")
			if (confs[v].colno == 0) {
				column0.push(eval("new " + confs[v].panelid + "()"));
			} else {
				column1.push(eval("new " + confs[v].panelid + "()"));
			}
		}
		this.portalPanel = {
			id : "Portal",
			xtype : "portal",
			region : "center",
			margins : "35 5 5 0",
			items : [{
						columnWidth : 0.65,
						style : "padding:10px 0 10px 10px",
						id : "PortalItem",
						items : column0
					}, {
						columnWidth : 0.35,
						style : "padding:10px 10px 10px 10px",
						items : column1
					}]
		};
	}
});
App.init = function() {
	var app = new AppHome({
				height : 600
			});
	app.render("main");
}
Ext.onReady(App.init);